package com.liu.shiro.config;

import com.liu.shiro.realm.CustomRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    /**
     * 自定义过滤器
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
        System.out.println("===== ShiroFilterFactoryBean 被执行 =======");

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 调用需要认证而未进行认证的接口时跳转的页面 ---- 默认Login.JSP
        shiroFilterFactoryBean.setLoginUrl("/pub/need_login");
        // 登录成功跳转的页面
        shiroFilterFactoryBean.setSuccessUrl("/");
        // 登录没权限跳转的接口  向验证登录 ——> 再验证权限
        shiroFilterFactoryBean.setUnauthorizedUrl("/pub/not_permit");

        // 拦截路径坑 ，hashmap 无序，导致部分路径无法拦截，因改为LinkedHashMap
        Map<String, String> filterChainDefinitionMap  = new LinkedHashMap<>();
        // 退出
        filterChainDefinitionMap.put("/logout","logout");
        // 匿名访问
        filterChainDefinitionMap.put("/pub/**","anon");
        // 登录用户访问
        filterChainDefinitionMap.put("/authc/**","authc");
        // admin角色可以访问
        filterChainDefinitionMap.put("/admin/**","roles[admin]");
        // 具有 video_update权限
        filterChainDefinitionMap.put("/video/update","perms[video_update]");
        // authc : url定义必须通过认证才可以访问
        // anon : url可以匿名访问
        // 坑二： 过滤链是按顺序执行的，从上而下，所以 /** 一般放最下面
        filterChainDefinitionMap.put("/**","authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    /**
     *
     * @return
     */
    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

        // 如果不是前后端分离，不用加
        securityManager.setSessionManager(sessionManager());
        // 推荐把setRealm放到最后
        securityManager.setRealm(customRealm());
        return securityManager;
    }

    @Bean
    public CustomRealm customRealm(){
        CustomRealm customRealm = new CustomRealm();
        //customRealm.setCredentialsMatcher(hashedCredentialsMatcher());

        return customRealm;
    }

    /**
     * 自定义realm加密
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        // 设置散列算法
        credentialsMatcher.setHashAlgorithmName("md5");
        // 散列次数。md5(md5(md5()))
        credentialsMatcher.setHashIterations(3);
        return credentialsMatcher;
    }

    /**
     * 自定义sessionManage
     *    设置session超时时间
     *    设置session缓存....等等
     * @return
     */
    @Bean
    public SessionManager sessionManager(){
        CustomSessionManage customSessionManage = new CustomSessionManage();
        // 设置session超时时间，默认30分钟（30分钟内无操作会过期，30分钟以内有操作会重新计时）
        customSessionManage.setGlobalSessionTimeout(20000);
        return customSessionManage;
    }

}
